package service

import (
	"context"
	"fmt"
	"github.com/shopspring/decimal"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _storeSortingService struct{}

func (s _storeSortingService) CreateApi(ctx context.Context, tx *dao.QueryTx, data *model.StoreSortingEntity) error {
	if err := tx.StoreSortingEntity.WithContext(ctx).Create(data); err != nil {
		return err
	}
	return nil
}

func (s _storeSortingService) GetStoreSortingPageApi(ctx context.Context, req bo.StoreSortingPageBoReq) (result []bo.StoreSortingPageBoResp, count int64, err error) {
	tx := dao.StoreSortingEntity.WithContext(ctx).WithContext(ctx).Where(dao.StoreSortingEntity.EnterpriseID.Eq(req.EnterpriseID))
	fmt.Println("req.OrderNo", req.OrderNo)
	if req.OrderNo != "" {
		tx = tx.Where(dao.StoreSortingEntity.OrderNo.Eq(req.OrderNo))
	}
	if req.StartTime != "" && req.EndTime != "" {
		tx = tx.Where(dao.StoreSortingEntity.CreatedAt.Between(utils.DateTimeStrToTime(req.StartTime), utils.DateTimeStrToTime(req.EndTime)))
	}
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	result = make([]bo.StoreSortingPageBoResp, 0)
	count, err = queryStoreSortingBuilder(tx).
		Order(dao.StoreSortingEntity.ID.Desc()).
		ScanByPage(&result, offset, limit)
	return
}

func (s _storeSortingService) GetStoreSortingBaseApi(ctx context.Context, id int64) (bo.StoreSortingPageBoResp, error) {
	tx := dao.StoreSortingEntity.WithContext(ctx).WithContext(ctx).Where(dao.StoreSortingEntity.ID.Eq(id))
	var result = bo.StoreSortingPageBoResp{}
	err := queryStoreSortingBuilder(tx).Scan(&result)
	return result, err
}

func (s _storeSortingService) GetStoreSortingByOrderIdApi(ctx context.Context, orderId int64) (*model.StoreSortingEntity, error) {
	return dao.StoreSortingEntity.WithContext(ctx).WithContext(ctx).Where(dao.StoreSortingEntity.OrdersID.Eq(orderId)).First()
}

func (s _storeSortingService) GetStoreSortingByOrderIdListApi(ctx context.Context, orderIdList []int64) ([]*model.StoreSortingEntity, error) {
	return dao.StoreSortingEntity.WithContext(ctx).WithContext(ctx).Where(dao.StoreSortingEntity.OrdersID.In(orderIdList...)).Find()
}

func (s _storeSortingService) ModifyByIdApi(ctx context.Context, tx *dao.QueryTx, id int64, outboundCount decimal.Decimal, amount decimal.Decimal) error {
	if _, err := tx.WithContext(ctx).StoreSortingEntity.Where(dao.StoreSortingEntity.ID.Eq(id)).UpdateSimple(
		dao.StoreSortingEntity.TotalCount.Value(outboundCount),
		dao.StoreSortingEntity.TotalAmount.Value(amount),
	); err != nil {
		return err
	}
	return nil
}

func (s _storeSortingService) DeleteByIdApi(ctx context.Context, tx *dao.QueryTx, id int64) error {
	if _, err := tx.StoreSortingEntity.WithContext(ctx).Where(dao.StoreSortingEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil

}

func (s _storeSortingService) GetStoreSortingByIdApi(ctx context.Context, id int64) (*model.StoreSortingEntity, error) {
	return dao.StoreSortingEntity.WithContext(ctx).Where(dao.StoreSortingEntity.ID.Eq(id)).First()
}

func queryStoreSortingBuilder(tx dao.IStoreSortingEntityDo) dao.IStoreSortingEntityDo {
	return tx.Select(
		dao.StoreSortingEntity.ALL,
		dao.EnterpriseCustomerEntity.Name.As("CustomerName"),
		dao.OrdersEntity.OrderNo.As("OrdersOrderNo"),
		dao.WarehouseEntity.Name.As("WarehouseName"),
	).
		LeftJoin(dao.EnterpriseCustomerEntity, dao.EnterpriseCustomerEntity.ID.EqCol(dao.StoreSortingEntity.CustomerID)).
		LeftJoin(dao.OrdersEntity, dao.OrdersEntity.ID.EqCol(dao.StoreSortingEntity.OrdersID)).
		LeftJoin(dao.WarehouseEntity, dao.WarehouseEntity.ID.EqCol(dao.StoreSortingEntity.WarehouseID))
}

var StoreSortingService = new(_storeSortingService)
